package com.tql.view.controller;

import com.tql.view.pojo.Scenery;
import com.tql.view.pojo.User;
import com.tql.view.service.SceneryService;
import com.tql.view.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.misc.BASE64Encoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/view")
public class ViewController {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private SceneryService sceneryService;
    /*
     * 展示图片信息
     * */
    @RequestMapping("/showDetails")
    public String showDetails(@RequestParam("location")String location,
                              @RequestParam("describe")String describe,
                              @RequestParam("createTime") Date createtime,
                              @RequestParam("name") String name,
                              @RequestParam("mytype")String mytype ,
                              @RequestParam("id")String id ,
                              Model model){
       /* String myId="ev"+id;*/
        //Redis 记录每一张图片浏览数
        Object num=redisUtil.get(id);
        if(num!=null){
            redisUtil.incr(id,1);
        }else{
            redisUtil.set(id,1);

        }
        sceneryService.updatePageView(id);
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>"+redisUtil.get(id));
        model.addAttribute("location",location);
        model.addAttribute("describes",describe);
        model.addAttribute("myname",name);
        model.addAttribute("mytype",mytype);
        model.addAttribute("id",id);
        SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        String createTime= simpleDateFormat.format(createtime);
        model.addAttribute("createTime",createTime);
        model.addAttribute("createtime",createtime);
        model.addAttribute("pageview",redisUtil.get(id));
        //获取评价数组
       /* List<Object> list=redisUtil.lGet(myId,0,-1);
        model.addAttribute("evaluate",list);*/
        return "view/viewDetails";
    }
    @RequestMapping(value="/download/{location}")
    public String downloads(@PathVariable("location")String location,
                            HttpServletResponse response , HttpServletRequest request) throws Exception{
        //要下载的图片地址
        String  path = request.getServletContext().getRealPath(location);
        String []split=location.split("/");
        String  fileName = UUID.randomUUID()+split[2];

        //1、设置response 响应头
        response.reset(); //设置页面不缓存,清空buffer
        response.setCharacterEncoding("UTF-8"); //字符编码
        response.setContentType("multipart/form-data"); //二进制传输数据
        //设置响应头
        response.setHeader("Content-Disposition",
                "attachment;fileName="+ URLEncoder.encode(fileName, "UTF-8"));

        File file = new File(path,fileName);
        //2、 读取文件--输入流
        InputStream input=new FileInputStream(file);
        //3、 写出文件--输出流
        OutputStream out = response.getOutputStream();

        byte[] buff =new byte[1024];
        int index=0;
        //4、执行 写出操作
        while((index= input.read(buff))!= -1){
            out.write(buff, 0, index);
            out.flush();
        }
        out.close();
        input.close();
        return null;
    }
    /*
图片评价处理
*/
    @RequestMapping("/updateEvaluate2")
    @ResponseBody
    public List<Object> updateEvaluate1(String evaluate,
                                        String id,
                                        HttpSession session){
        //生成唯一key,List集合存放评价
        String myid="369"+id;
        String  str="";
        //右边push

        /*List<Object> list=redisUtil.lGet(myId,0,-1);
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+list);*/
        User user= (User) session.getAttribute("visitor");
        if(evaluate!=null&&!evaluate.equals("")){
            str =user.getUsername()+":"+evaluate;
        }
        List<Object> list1=redisUtil.lGet(myid,0,-1);
        if(list1!=null){
            redisUtil.lpush(myid,str);
        }else{
            redisUtil.lpush(myid,str);
        }
        list1=redisUtil.lGet(myid,0,-1);
        return list1;
    }
}
